package cn.dnui_cr503.dnuifood_16110100503.controller;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.adapter.SearchAdapter;
import cn.dnui_cr503.dnuifood_16110100503.bean.Searchbean;
import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.model.SearchModel;

public class SearchActivity extends AppCompatActivity implements OnRetrofitListener {
    private String search;
    private List<Searchbean> searchlist;
    private RecyclerView recyclerView;
    private SearchAdapter searchAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        search =  getIntent().getStringExtra("a");

        recyclerView=findViewById(R.id.search_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        initDate();
    }

    private void initDate() {
        SearchModel searchModel = new SearchModel();
        searchModel.getFoodBySearch(search,this);//访问服务器请求
    }


    @Override
    public void onSuccess(Object object, int flag) {
        searchlist=(List<Searchbean>) object;
        searchAdapter = new SearchAdapter(this, searchlist);
        recyclerView.setAdapter(searchAdapter);
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(SearchActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
    }
}
